define([ "app", "jquery", "md5", "ecb", 
         "directives/DatetimePicker",
 		 "directives/dataRecords/ShowDetail",
 		 "directives/addCompany/AddCompany", 
         "services/UserService",
		 "resources/UserResource", 
		 "resources/DataRecordResource"
		],
		function(app) {

	var deps = [ "$scope", "$state", "$window",  "$location",
			"$stateParams", "MenuService", "UserService", "UserResource","DataRecordResource" ];

	function controller($scope, $state, $window, $location,
			$stateParams, MenuService, UserService, UserResource,DataRecordResource) {
		$scope.currentPage = 1;
        $scope.pages = [];
        $scope.pageSize = 10;
        
		// 初始化查询类型,查询直拨+回拨
		$scope.tabChoose=3;
		// 默认选择电话会议
		$scope.currentDate = strToDate(new Date());
		//刷新页面初始化时，确定当前菜单
		$scope.currentMenuItem = MenuService.queryByLocationURL($location.$$url);
		// 通过go传递参数-主键ID 
		var keyId=$stateParams.keyId;
		$scope.user ={};
		$scope.user = UserService.getCurrentUser();
		if(keyId==null || keyId==""){
			keyId =$scope.user.currentCompanyCode;
		}
		initDate(keyId);
		
		$scope.changeLogoutImage =function (flag){
			var image =document.getElementById('imageChanges');
			if(flag){
				image.src="images/logout2.png";
			}else{
				image.src="images/logout1.png";
			}
		};
		
		$scope.changeChargeImage=function (flag){
			var image =document.getElementById('changeChargeImage');
			if(flag){
				image.src="images/charge2.png";
			}else{
				image.src="images/charge1.png";
			}
		};
		$scope.changeAddImage=function (flag){
			var image =document.getElementById('changeAddImage');
			if(flag){
				image.src="images/add2.png";
			}else{
				image.src="images/add1.png";
			}
		};
		$scope.changeMessageImage = function(flag) {
			var image =document.getElementById('changeMessageImage');
			if(flag){
				image.src="images/message2.png";
			}else{
				image.src="images/message1.png";
			}
		};
		
		$scope.changeDate =function ($date){
			//$scope.currentDate = $date;
			
			DataRecordResource.queryCumulativeComsumeList(keyId,$scope.tabChoose).success(function(data){
				$scope.consume = data;
            });
			// 获取消费明细
			DataRecordResource.queryCumulativeDetailList(keyId,$scope.tabChoose,convertDate($scope.currentDate),$scope.currentPage,$scope.pageSize).success(function(data){
				$scope.data = data.dataRecordList;
				if(data.count % $scope.pageSize == 0){
         			$scope.totalPage=Math.floor(data.count/$scope.pageSize);
        		}else{
        			$scope.totalPage=Math.floor(data.count/$scope.pageSize)+1;
        		}
            });
		};
		
		function queryData(keyId,searchKey){
			DataRecordResource.queryCumulativeDetailList(keyId,$scope.tabChoose,convertDate($scope.currentDate),$scope.currentPage,$scope.pageSize,searchKey).success(function(data){
				$scope.data = data.dataRecordList;
				if(data.count % $scope.pageSize == 0){
         			$scope.totalPage=Math.floor(data.count/$scope.pageSize);
        		}else{
        			$scope.totalPage=Math.floor(data.count/$scope.pageSize)+1;
        		}
            });
		}
		
		$scope.gotoPage = function (currentNum) {
			
			if($scope.currentPage != currentNum){
				 $scope.currentPage = currentNum;
		            queryData(keyId,$scope.searchKey);
			}
        };
        
        $scope.nextPage = function () {
        	if($scope.currentPage<$scope.totalPage){
        		$scope.currentPage++;
        		queryData(keyId,$scope.searchKey);
        	}
        };

        $scope.upPage = function () {
        	if($scope.currentPage>1){
        		$scope.currentPage--;
        		queryData(keyId,$scope.searchKey);
        	}	
        };
        
        
        /**
         * 输入关键词查询
         */
        $scope.search =function (){
        	$scope.currentPage=1;
        	queryData(keyId,$scope.searchKey);
        }
        
        /**
         * tab 选择方法 1:查询直拨+回拨记录 2:查询会议记录
         */
		$scope.selectType =function (flag){
			if(flag==2){
				$scope.tabChoose=2;
				$("#confernece_btn").addClass("confernece_btn_1");
				$("#confernece_btn1").addClass("confernece_btn1_1");
			}
			if(flag==1){
				$scope.tabChoose=3;
				$("#confernece_btn").removeClass("confernece_btn_1");
				$("#confernece_btn1").removeClass("confernece_btn1_1");
				$("#confernece_btn").addClass("confernece_btn");
				$("#confernece_btn1").addClass("confernece_btn1");
			}
			
			DataRecordResource.queryCumulativeComsumeList(keyId,$scope.tabChoose).success(function(data){
				$scope.consume = data;
            });
			
			// 获取消费明细
			DataRecordResource.queryCumulativeDetailList(keyId,$scope.tabChoose,convertDate($scope.currentDate),$scope.currentPage,$scope.pageSize).success(function(data){
				$scope.data = data.dataRecordList;
				if(data.count % $scope.pageSize == 0){
         			$scope.totalPage=Math.floor(data.count/$scope.pageSize);
        		}else{
        			$scope.totalPage=Math.floor(data.count/$scope.pageSize)+1;
        		}
            });
		};
		
		/**
		 * 获取数据
		 */
		function initDate(keyId){
			var currentDate =$scope.currentDate;
			// 获取消费记录
			DataRecordResource.queryCumulativeComsumeList(keyId,$scope.tabChoose).success(function(data){
				$scope.consume = data;
            });
			
			// 获取消费明细
			DataRecordResource.queryCumulativeDetailList(keyId,$scope.tabChoose,convertDate(currentDate),$scope.currentPage,$scope.pageSize).success(function(data){
				$scope.data = data.dataRecordList;
				if(data.count % $scope.pageSize == 0){
         			$scope.totalPage=Math.floor(data.count/$scope.pageSize);
        		}else{
        			$scope.totalPage=Math.floor(data.count/$scope.pageSize)+1;
        		}
            });
		}
	}
	
	function strToDate(str) {
		return new Date(str).format("yyyy-MM-dd");
	}
	
	
	function convertDate(dateStr) {
    	var timeStamp = null;
    	if(dateStr) {
    		var newDateStr = dateStr + ' 00:00:00';
    		newDateStr = newDateStr.replace(/-/g,"/");
    		var date = new Date(newDateStr);
    		timeStamp = date.getTime();
    	}
    	return timeStamp;
    }
	Date.prototype.format =function(format) {
        var o = {
            "M+" : this.getMonth()+1, //month
		    "d+" : this.getDate(),    //day
		    "h+" : this.getHours(),   //hour
		    "m+" : this.getMinutes(), //minute
		    "s+" : this.getSeconds(), //second
		    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
		    "S" : this.getMilliseconds() //millisecond
        };
        if(/(y+)/.test(format)) 
        	format=format.replace(RegExp.$1, (this.getFullYear()+"").substr(4- RegExp.$1.length));
        for(var k in o)
        	if(new RegExp("("+ k +")").test(format))
        		format = format.replace(RegExp.$1, RegExp.$1.length==1? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
        return format;
    };
	
	controller.$inject = deps;
	return app.lazy.controller("DataRecordController", controller);
});
